<?php
namespace app\restaurant\model;

use app\BaseModel;
use think\facade\Db;

class Coupon extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_restaurant_coupon';

    protected $coupon_limit_model;


    protected $append = [

        'store',

        'cate',

        'goods',

        'end_time_text'

    ];


    /**
     * @param $value
     * @param $data
     * @功能说明:
     * @author chenniang
     * @DataTime: 2020-10-21 10:48
     */
    public function getEndTimeTextAttr($value,$data){

        if(isset($data['time_type'])&&isset($data['start_time'])&&isset($data['end_time'])&&isset($data['day'])){

            if($data['time_type']==0){

                return date('Y-m-d H:i:s',$data['start_time']).'~'.date('Y-m-d H:i:s',$data['end_time']);

            }else{

                return $data['day'].'天后过期';
            }
        }

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-21 13:46
     * @功能说明:判断是否过期
     */
    public function getStatusAttr($value,$data){

        if(isset($data['time_type'])&&isset($data['end_time'])&&$data['time_type']==0&&$data['end_time']<time()){

            $value = 2;
        }

        return $value;
    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-10 17:10
     * @功能说明:门店
     */
    public function getStoreAttr($value,$data){

        if(!empty($data['id'])){

            $arr = $this->getDataSelect($data['id'],3);

            return $arr;

        }

    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-10 17:10
     * @功能说明:门店
     */
    public function getCateAttr($value,$data){

        if(!empty($data['id'])){

            $arr = $this->getDataSelect($data['id'],1);

            return $arr;

        }

    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-10 17:10
     * @功能说明:门店
     */
    public function getGoodsAttr($value,$data){

        if(!empty($data['id'])){

            $arr = $this->getDataSelect($data['id'],2);

            return $arr;

        }

    }


    /**
     * @param $id
     * @param $type
     * @功能说明:
     * @author chenniang
     * @DataTime: 2020-10-10 17:24
     */

    public function getDataSelect($id,$type){


        if(empty($this->coupon_limit_model)){

            $this->coupon_limit_model = new CouponLimit();

        }

        $dis = [

            'a.coupon_id' => $id,

            'a.type'      => $type,

            'a.is_record' => 0
        ];

        $arr = $this->coupon_limit_model->getDataSelect($type,$dis);

        return $arr;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:04
     * @功能说明:添加
     */
    public function couponAdd($data){

        $data['create_time'] = time();

        $data['update_time'] = time();

        $store = [];

        $cate  = [];

        $goods = [];

        if(isset($data['store'])){

            $store = $data['store'];

            unset($data['store']);

        }

        if(isset($data['cate'])){

            $cate = $data['cate'];

            unset($data['cate']);
        }

        if(isset($data['goods'])){

            $goods = $data['goods'];

            unset($data['goods']);
        }

        $res = $this->insert($data);

        $coupon_id = $this->getLastInsID();

        $this->updateSome($store,$cate,$goods,$data['uniacid'],$coupon_id,$data);

        return $res;

    }



    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:05
     * @功能说明:编辑
     */
    public function couponUpdate($dis,$data){

        $data['update_time'] = time();

        $store = [];

        $cate  = [];

        $goods = [];

        if(isset($data['store'])){

            $store = $data['store'];

            unset($data['store']);

        }

        if(isset($data['cate'])){

            $cate = $data['cate'];

            unset($data['cate']);
        }

        if(isset($data['goods'])){

            $goods = $data['goods'];

            unset($data['goods']);
        }

        $res = $this->where($dis)->update($data);

        $this->updateSome($store,$cate,$goods,$data['uniacid'],$data['id'],$data);

        return $res;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:06
     * @功能说明:列表
     */
    public function couponList($dis,$page){

        $data = $this->where($dis)->order('top desc,id desc')->paginate($page)->toArray();

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-29 11:43
     * @功能说明:
     */
    public function couponInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():[];

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-09 17:51
     * @功能说明:
     */

    public function updateSome($store,$cate,$goods_id,$uniacid,$coupon_id,$data){

        $limit_model = new CouponLimit();

        if($data['all_store']==0){
            //门店
            $limit_model->couponAdd($store,3,$uniacid,$coupon_id);
        }

        if($data['limit_type']==1){
            //分类
            $limit_model->couponAdd($cate,1,$uniacid,$coupon_id);
        }

        if($data['limit_type']==2){
             //商品
            $limit_model->couponAdd($goods_id,2,$uniacid,$coupon_id);
        }

        return true;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-20 15:18
     * @功能说明:可以领取对优惠券
     */
    public function couponCanGetPage($uniacid,$user_id){

        $id = $this->canGetId($uniacid,$user_id);

        $dis[] = ['id','in',$id];

        $data = $this->where($dis)->field('*,discount as reduce')->order('id desc')->paginate(10)->toArray();

        $record_model = new CouponRecord();

        if(!empty($data['data'])){

            foreach ($data['data'] as &$v){

                $where[] = ['coupon_id','=',$v['id']];

                $where[] = ['user_id','=',$user_id];

                $num = $record_model->where($where)->count();

                $v['can_get_num'] = $v['limit_num'] - $num>0?$v['limit_num'] - $num:0;

            }
        }

        return $data;

    }


    /**
     * @author chenniang
     * @DataTime: 2020-10-20 15:18
     * @功能说明:可以领取对优惠券
     */
    public function couponCanGetlimit($uniacid,$user_id){

        $id = $this->canGetId($uniacid,$user_id);

        $dis[]= ['id','in',$id];

        $record_model = new CouponRecord();

        $data = $this->where($dis)->field('*,discount as reduce')->limit(5)->select()->toArray();

        if(!empty($data)){

            foreach ($data as &$v){

                $where[] = ['coupon_id','=',$v['id']];

                $where[] = ['user_id','=',$user_id];

                $num = $record_model->where($where)->count();

                $v['can_get_num'] = $v['limit_num'] - $num>0?$v['limit_num'] - $num:0;

            }
        }

        return $data;

    }

    /**
     * @author chenniang
     * @DataTime: 2020-10-20 15:23
     * @功能说明:已经领取过对优惠券
     */
    public function canGetId($uniacid,$user_id){

        $dis['status']  = 1;

        $dis['uniacid'] = $uniacid;

        $data = $this->where($dis)->where('stock','>',0)->select()->toArray();

        $record_model = new CouponRecord();

        if(!empty($data)){

            foreach ($data as $k=>$v){

                $where = [];

                $where[] = ['coupon_id','=',$v['id']];

                $where[] = ['user_id','=',$user_id];

                $num = $record_model->where($where)->count();

                $num = is_numeric($num)?$num:0;

                if($v['limit_num']<=$num){

                    unset($data[$k]);
                }

                if($v['time_type']==0&&$v['end_time']<time()){

                    unset($data[$k]);

                }
            }

        }

        $id = !empty($data)?array_column($data,'id'):[];

        return $id;
    }








}